#include "utils.h"
#include <pthread.h>

void *thread_func_static_barrier(void *param) {
    long long t_id = (long long)param;
    for (int k = 0; k < n; k++) {
        if (t_id == 0) {
            float ele = A[k][k];
            for (int j = k + 1; j < n; j++) A[k][j] /= ele;
            A[k][k] = 1.0;
        }
        pthread_barrier_wait(&barrier);
        for (int i = k + t_id + 1; i < n; i += thread_count) {
            for (int j = k + 1; j < n; j++) A[i][j] -= A[i][k] * A[k][j];
            A[i][k] = 0;
        }
        pthread_barrier_wait(&barrier);
    }
    pthread_exit(NULL);
    return NULL;
}
// 静态线程 barrier 优化的高斯消去法
void static_gauss_barrier() {
    pthread_barrier_init(&barrier, NULL, thread_count);
    pthread_t *handles = new pthread_t[thread_count];
    for (int i = 0; i < thread_count; i++) pthread_create(&handles[i], NULL, thread_func_static_barrier, (void *)(long long)i);
    for (int i = 0; i < thread_count; i++) pthread_join(handles[i], NULL);
    pthread_barrier_destroy(&barrier);
    delete[] handles;
}
